package com.glory.gloryquickui.utils

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.util.DisplayMetrics
import com.glory.gloryquickui.R
import java.io.*

object Utils {
    fun getString(inputStream: InputStream?): String {
        var inputStreamReader: InputStreamReader? = null
        try {
            inputStreamReader = InputStreamReader(inputStream, "utf-8")
        } catch (var6: UnsupportedEncodingException) {
            var6.printStackTrace()
        }
        val reader = BufferedReader(inputStreamReader)
        val sb = StringBuffer("")
        var line: String?
        try {
            while (reader.readLine().also { line = it } != null) {
                sb.append(line)
                sb.append("\n")
            }
        } catch (var7: IOException) {
            var7.printStackTrace()
        }
        return sb.toString()
    }

    // 屏幕高度（像素）
    fun getWindowHeight(activity: Activity): Int {
        val metric = DisplayMetrics()
        activity.windowManager.defaultDisplay.getMetrics(metric)
        return metric.heightPixels
    }

    /**
     * 通过图片名字获取对应得drawable ，默认图片删除及时更换
     */
    fun getPicToDrawable(context: Context?, picName: String): Drawable {
        return if (picName.isNotEmpty()) {
            try {
                val drawable = context?.getDrawable(context.resources
                    .getIdentifier(picName, "drawable", context.packageName))
                drawable!!
            }catch (e : Exception){
                context?.getDrawable(R.drawable.search)!!
            }
        }else (
            context?.getDrawable(R.drawable.search)!!
        )
    }
}